package com.ruoyi.web.controller.front;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.FrontPage;
import com.ruoyi.common.enums.FrontMenuEnum;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TArticle;
import com.ruoyi.system.domain.TCompanyInfo;
import com.ruoyi.system.domain.TFrontMenu;
import com.ruoyi.system.domain.TMessage;
import com.ruoyi.system.service.ITArticleService;
import com.ruoyi.system.service.ITCompanyInfoService;
import com.ruoyi.system.service.ITFrontMenuService;
import com.ruoyi.system.service.ITMessageService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * @Author: likailun
 * @Description:
 * @Date: create in 2024/4/17 19:58
 */

@Controller
@RequestMapping("/front")
public class FrontController extends BaseController {
    private final static String prefix = "front";

    @Value("${system.name}")
    private String systemName;
    @Autowired
    private ITFrontMenuService tFrontMenuService;
    @Resource
    private ITArticleService articleService;
    @Resource
    private ITCompanyInfoService companyInfoService;
    @Resource
    private ITMessageService messageService;

    @GetMapping("/{categorize}")
    public String path(@PathVariable("categorize") String categorize, String category, ModelMap mmap,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                       @RequestParam(value = "pageNum", required = false) Integer pageNum) {
        AtomicReference<String> active = new AtomicReference<>("/front/index");
        final TCompanyInfo companyInfo = this.getCompanyInfo();
        StringBuilder title = new StringBuilder(StringUtils.isEmpty(companyInfo.getCompanyName()) ? systemName : companyInfo.getCompanyName());
        TFrontMenu condition = new TFrontMenu();
        condition.setIsDisplay(1L);
        List<TFrontMenu> tFrontMenus = tFrontMenuService.selectTFrontMenuList(condition);
        List<TFrontMenu> menus = new ArrayList<>();
        List<TFrontMenu> currentChildren = new ArrayList<>();
        TFrontMenu current = new TFrontMenu();
        AtomicReference<String> activeChild = new AtomicReference<>("");
        if (CollectionUtils.isNotEmpty(tFrontMenus)) {
            for (TFrontMenu tFrontMenu : tFrontMenus) {
                if (tFrontMenu.getPageUrl().startsWith("/front/" + categorize)) {
                    active.set("/front/" + categorize);
                    title.append("|").append(tFrontMenu.getMenuName());

                    String pageUrl="/front/" + categorize;
                    if(StringUtils.isNotEmpty(category) ){
                        pageUrl+="?category" + "=" + category;
                    }
                    if (tFrontMenu.getPageUrl().equals(pageUrl)) {
                        activeChild.set(tFrontMenu.getPageUrl());
                        current = tFrontMenu;
                    }
                }
            }
            tFrontMenus.stream().filter(t -> t.getParentId() == 0L).forEach(t -> {
                List<TFrontMenu> collect = tFrontMenus.stream().filter(c -> c.getParentId().equals(t.getId())).peek(c -> c.setRootUrl(c.getPageUrl().split("\\?")[0])).collect(Collectors.toList());
                t.setChilds(collect);
                t.setRootUrl(t.getPageUrl().split("\\?")[0]);
                t.setActive(t.getRootUrl().equals("/front/" + categorize) ? 1 : 0);
                if (CollectionUtils.isNotEmpty(collect)) {
                    if (StringUtils.isEmpty(activeChild.get())) {
                        activeChild.set(collect.get(0).getPageUrl());
                    }
                    if (t.getActive() == 1) {
                        for (TFrontMenu tFrontMenu : collect) {
                            if (StringUtils.isNotEmpty(activeChild.get()) && tFrontMenu.getPageUrl().equals(activeChild.get())) {
                                tFrontMenu.setActive(1);
                            }
                        }
                        currentChildren.addAll(collect);
                    }
                }
                menus.add(t);
            });
        }
        mmap.put("activeChild", activeChild.get());
        mmap.put("systemName", systemName);
        mmap.put("title", title.toString());
        mmap.put("activeParent", active.get());
        mmap.put("menus", menus);
        mmap.put("company", companyInfo);
        if (CollectionUtils.isNotEmpty(tFrontMenus)) {
            Optional<TFrontMenu> menu = Optional.empty();
            if (StringUtils.isNotEmpty(activeChild.get())) {
                // 选中子菜单
                menu = tFrontMenus.stream().filter(m -> m.getPageUrl().equals(activeChild.get())).findAny();
            } else if (StringUtils.isNotEmpty(active.get())) {
                menu = tFrontMenus.stream().filter(m -> m.getPageUrl().equals(active.get())).findAny();
            }
            if (menu.isPresent()) {
                mmap.put("url", menu.get().getPageUrl());
                this.queryFrontData(categorize, category, menu.get(), tFrontMenus, mmap, pageSize, pageNum);
            }
        }
        mmap.put("current", current);
        mmap.put("currentChildren", currentChildren);
        // 为动态页面传递当前菜单ID
        if ("dynamics".equals(categorize)) {
            mmap.put("currentMenuId", current.getId());
        }
        return frontPage(categorize, category);
    }

    private String frontPage(String categorize, String category) {
        if (categorize.equals("heixie") || categorize.equals("dynamics")) {
            if (StringUtils.isNotEmpty(category)) {
                return prefix + "/" + categorize + "-" + category;
            } else {
                return prefix + "/" + categorize + "-" + defaultCategory(categorize);
            }
        }
        return prefix + "/" + categorize;

    }

    private String defaultCategory(String categorize) {
        switch (categorize) {
            case "heixie":
                return "zjhx";
            case "dynamics":
                return "hedt";
            default:
                return "";
        }
    }

    @GetMapping("/articleView/{id}")
    public String articleView(@PathVariable("id") Long id, ModelMap mmap) {
        TArticle article = articleService.selectTArticleById(id);
        if (Objects.isNull(article)) {
            // 查不到数据，直接去首页了
            return "redirect:/front/index";
        }
        AtomicReference<String> active = new AtomicReference<>("/front/index");
        final TCompanyInfo companyInfo = this.getCompanyInfo();
        StringBuilder title = new StringBuilder(StringUtils.isEmpty(companyInfo.getCompanyName()) ? systemName : companyInfo.getCompanyName());
        TFrontMenu condition = new TFrontMenu();
        condition.setIsDisplay(1L);
        List<TFrontMenu> allMenus = tFrontMenuService.selectTFrontMenuList(condition);
        List<TFrontMenu> menus = new ArrayList<>();
        AtomicReference<String> activeChild = new AtomicReference<>("");
        Optional<TFrontMenu> menu = Optional.empty();
        if (CollectionUtils.isNotEmpty(allMenus)) {
            menu = allMenus.stream().filter(m -> m.getId().equals(article.getFrontMenuId())).findAny();
            List<TFrontMenu> parents = new ArrayList<>();
            if (menu.isPresent()) {
                this.parents(menu.get(), allMenus, parents);
                mmap.put("current", menu.get());
                title.append("|").append(menu.get().getMenuName());
                activeChild.set(menu.get().getPageUrl());
                mmap.put("parent", parents);
                Optional<TFrontMenu> finalMenu = menu;
                allMenus.stream().filter(t -> t.getParentId() == 0L).forEach(t -> {
                    List<TFrontMenu> collect = allMenus.stream().filter(c -> c.getParentId().equals(t.getId())).peek(c -> c.setRootUrl(c.getPageUrl().split("\\?")[0])).collect(Collectors.toList());
                    t.setChilds(collect);
                    t.setRootUrl(t.getPageUrl().split("\\?")[0]);
                    t.setActive(t.getRootUrl().equals(finalMenu.get().getPageUrl().split("\\?")[0]) ? 1 : 0);
                    menus.add(t);
                });
            }
        }
        if (menu.isPresent()) {
            final Map<String, Long> stringLongMap = articleService.queryPrevAndNext(id, menu.get().getId());
            final long prevId = MapUtils.getLongValue(stringLongMap, "prev_id", 0L);
            if (prevId > 0) {
                // 上一篇
                mmap.put("prev", articleService.selectTArticleById(prevId));
            }
            final long nextId = MapUtils.getLongValue(stringLongMap, "next_id", 0L);
            if (nextId > 0) {
                // 上一篇
                mmap.put("next", articleService.selectTArticleById(nextId));
            }
        }
        final TArticle tArticle = new TArticle();
        tArticle.setId(id);
        tArticle.setViewCount(Objects.isNull(article.getViewCount()) ? 1 : article.getViewCount() + 1);
        articleService.updateTArticle(tArticle);
        article.setViewCount(tArticle.getViewCount());
        mmap.put("article", article);
        mmap.put("activeChild", activeChild.get());
        mmap.put("systemName", systemName);
        mmap.put("title", title.toString());
        mmap.put("activeParent", active.get());
        mmap.put("menus", menus);
        mmap.put("company", companyInfo);
        return prefix + "/articleView";
    }


    @PostMapping("/contact/submit")
    public String saveMessage(TMessage message, ModelMap mmap) {
        message.setCreateTime(new Date());
        message.setHandlingStatus(0);
        messageService.insertTMessage(message);

        AtomicReference<String> active = new AtomicReference<>("/front/index");
        final TCompanyInfo companyInfo = this.getCompanyInfo();
        StringBuilder title = new StringBuilder(StringUtils.isEmpty(companyInfo.getCompanyName()) ? systemName : companyInfo.getCompanyName());
        TFrontMenu condition = new TFrontMenu();
        condition.setIsDisplay(1L);
        List<TFrontMenu> allMenus = tFrontMenuService.selectTFrontMenuList(condition);
        List<TFrontMenu> menus = new ArrayList<>();
        AtomicReference<String> activeChild = new AtomicReference<>("");
        Optional<TFrontMenu> menu = Optional.empty();
        if (CollectionUtils.isNotEmpty(allMenus)) {
            menu = allMenus.stream().filter(m -> m.getMenuName().equals("联系我们")).findAny();
            if (menu.isPresent()) {
                mmap.put("current", menu.get());
                title.append("|").append(menu.get().getMenuName());
                activeChild.set(menu.get().getPageUrl());
                Optional<TFrontMenu> finalMenu = menu;
                allMenus.stream().filter(t -> t.getParentId() == 0L).forEach(t -> {
                    List<TFrontMenu> collect = allMenus.stream().filter(c -> c.getParentId().equals(t.getId())).peek(c -> c.setRootUrl(c.getPageUrl().split("\\?")[0])).collect(Collectors.toList());
                    t.setChilds(collect);
                    t.setRootUrl(t.getPageUrl().split("\\?")[0]);
                    t.setActive(t.getRootUrl().equals(finalMenu.get().getPageUrl().split("\\?")[0]) ? 1 : 0);
                    menus.add(t);
                });
            }
        }
        mmap.put("activeChild", activeChild.get());
        mmap.put("systemName", systemName);
        mmap.put("title", title.toString());
        mmap.put("activeParent", active.get());
        mmap.put("menus", menus);
        mmap.put("company", companyInfo);
        return prefix + "/contact-sucess";
    }

    /**
     * ajax 查询明细
     *
     * @return
     */
    @GetMapping(value = "/teamDetail/{id}")
    @ResponseBody
    public ResponseEntity<TArticle> getTeamDetail(@PathVariable("id") Long id) {
        TArticle article = articleService.selectTArticleById(id);
        return ResponseEntity.ok(article);
    }


    private void parents(TFrontMenu current, List<TFrontMenu> tFrontMenus, List<TFrontMenu> parents) {
        parents.add(current);
        final Optional<TFrontMenu> parent = tFrontMenus.stream().filter(m -> m.getId().equals(current.getParentId())).findFirst();
        if (parent.isPresent()) {
            if (parent.get().getParentId() > 0) {
                this.parents(parent.get(), tFrontMenus, parents);
            } else {
                parents.add(0, parent.get());
            }
        }

    }

    private void queryFrontData(String categorize, String category, TFrontMenu menu, List<TFrontMenu> menus, ModelMap mmap, Integer pageSize, Integer pageNum) {
        FrontMenuEnum menuEnum = FrontMenuEnum.getByCategorize(categorize);
        if (Objects.isNull(menuEnum)) {
            return;
        }
        Long menuId=menu.getId();
        if(CollectionUtils.isNotEmpty(menu.getChilds()) && menu.getChilds().stream().anyMatch(c->null!=c.getActive() && c.getActive().equals(1))){
            menuId=menu.getChilds().stream().filter(c->null!=c.getActive() && c.getActive().equals(1)).findFirst().get().getId();
        }
        switch (menuEnum) {
            case INDEX:
                // 公司简介
                List<TArticle> gsjj = getIndexPageArticle(menus, "机构简介", 1, 1);
                mmap.put("gsjj", CollectionUtils.isNotEmpty(gsjj)?gsjj.get(0):new TArticle());

                // 产品中心
                mmap.put("services", getIndexPageArticle(menus, "服务领域", 1, 4));
                mmap.put("products", getIndexPageArticle(menus, "服务领域", 1, 6));

                final List<TArticle> about = getIndexPageArticle(menus, "关于我们", 1, 1);
                mmap.put("about", CollectionUtils.isNotEmpty(about) ? about.get(0) : new TArticle());
                List<TArticle> hxdt = getIndexPageArticle(menus, "黑蝎动态", 1, 3);
                List<TArticle> zsdt = getIndexPageArticle(menus, "招商动态", 1, 3);
                hxdt.addAll(zsdt);
                mmap.put("dynamics", hxdt.stream().sorted(Comparator.comparing(TArticle::getCreateTime, Comparator.reverseOrder())).collect(Collectors.toList()));

                // 成功展示
                mmap.put("achievements", getIndexPageArticle(menus, "服务领域", 1, 3));
                break;
            case HEIXIE:
            case DYNAMICS:
            case SERVICE:
            case TEAM:
                if (FrontMenuEnum.TEAM!=menuEnum &&( StringUtils.isEmpty(category) || category.equals("zjhx")
                        || category.equals("ywbj")|| category.equals("hzhb"))) {
                    // 走进黑蝎、业务布局、业务布局(地图)
                    startPage(1, 1);
                    TArticle ta = new TArticle();
                    ta.setFrontMenuId(menuId);
                    List<TArticle> tas = articleService.selectTArticleList(ta);
                    mmap.put("article", CollectionUtils.isNotEmpty(tas) ? tas.get(0) : null);
                } else if (StringUtils.isNotEmpty(category) && category.equals("zzjg")) {
                    // 组织架构 - 查询子公司列表
                    if (null == pageSize) {
                        pageSize = 8;
                    }
                    if (pageNum == null) {
                        pageNum = 1;
                    }
                    startPage(pageNum, pageSize);
                    TArticle article = new TArticle();
                    article.setFrontMenuId(menuId);
                    article.setIsDisplay(1L);
                    List<TArticle> tArticles = articleService.selectTArticleList(article);
                    mmap.put("articleList", tArticles.stream().sorted(Comparator.comparing(TArticle::getSort)).collect(Collectors.toList()));
                    FrontPage page = getFrontPage(tArticles);
                    mmap.put("page", page);
                } else {
                    //
                    if (null == pageSize) {
                        pageSize = 9;
                    }
                    if (pageNum == null) {
                        pageNum = 1;
                    }
                    if(StringUtils.isNotEmpty(category) && (category.equals("hxzl") ||category.equals("ywbjdt"))){
                        pageSize = 1000;
                        pageNum = 1;
                    }
                    startPage(pageNum, pageSize);
                    TArticle article = new TArticle();
                    article.setFrontMenuId(menuId);
                    List<TArticle> tArticles = articleService.selectTArticleList(article);
                    FrontPage page = getFrontPage(tArticles);
                    mmap.put("page", page);
                }

                break;
            case ACHIEVEMENT:
                TArticle article = new TArticle();
                article.setFrontMenuId(menuId);
                List<TArticle> tArticles = articleService.selectTArticleList(article);
                Map<String, List<TArticle>> achievementMap = new HashMap<>();
                achievementMap.put("dt-tech",tArticles.stream().filter(t->t.getArticleVideo().equals("dt-tech")).collect(Collectors.toList()));
                achievementMap.put("data-analysis",tArticles.stream().filter(t->t.getArticleVideo().equals("data-analysis")).collect(Collectors.toList()));
                achievementMap.put("investment-service",tArticles.stream().filter(t->t.getArticleVideo().equals("investment-service")).collect(Collectors.toList()));
                achievementMap.put("project-packaging",tArticles.stream().filter(t->t.getArticleVideo().equals("project-packaging")).collect(Collectors.toList()));
                achievementMap.put("rural-revitalization",tArticles.stream().filter(t->t.getArticleVideo().equals("rural-revitalization")).collect(Collectors.toList()));
                achievementMap.put("park-incubation",tArticles.stream().filter(t->t.getArticleVideo().equals("park-incubation")).collect(Collectors.toList()));
                achievementMap.put("media-design",tArticles.stream().filter(t->t.getArticleVideo().equals("media-design")).collect(Collectors.toList()));
                achievementMap.put("graphic-design",tArticles.stream().filter(t->t.getArticleVideo().equals("graphic-design")).collect(Collectors.toList()));
                mmap.put("achievement", achievementMap);
                break;
            case CONTACT:
                startPage(1, 1);
                TArticle ta = new TArticle();
                ta.setFrontMenuId(menu.getId());
                List<TArticle> tas = articleService.selectTArticleList(ta);
                mmap.put("article", CollectionUtils.isNotEmpty(tas) ? tas.get(0) : null);
                break;
        }
    }

    /**
     * 查询首页部分的文章
     *
     * @param menus
     */
    private List<TArticle> getIndexPageArticle(List<TFrontMenu> menus, String menuName, int pageSize, int pageNumber) {
        List<TArticle> tArticles = new ArrayList<>();
        startPage(pageSize, pageNumber);
        TArticle productCon = new TArticle();
        final Optional<TFrontMenu> productMenu = menus.stream().filter(m -> m.getMenuName().equals(menuName)).findAny();
        if (productMenu.isPresent()) {
            final HashMap<String, Object> params = new HashMap<>();
            params.put("frontMenuIds", new ArrayList<Long>() {{
                add(productMenu.get().getId());
                if (CollectionUtils.isNotEmpty(productMenu.get().getChilds())) {
                    addAll(productMenu.get().getChilds().stream().map(TFrontMenu::getId).collect(Collectors.toSet()));
                }
            }});
            productCon.setParams(params);
            productCon.setIsDisplay(1L);
            tArticles = articleService.selectTArticleList(productCon);
            for (TArticle tArticle : tArticles) {
                tArticle.setFrontMenuName(menus.stream().filter(m -> m.getId().equals(tArticle.getFrontMenuId())).findFirst().get().getMenuName());
            }
        }
        return tArticles;
    }
    //private

    @RequestMapping("/listArticle")
    @ResponseBody
    public ResponseEntity<?> listArticle(TArticle article) {
        List<TArticle> list = articleService.selectTArticleList(article);
        return ResponseEntity.ok(list);
    }

    private TCompanyInfo getCompanyInfo() {
        final List<TCompanyInfo> tCompanyInfos = companyInfoService.selectTCompanyInfoList(new TCompanyInfo());
        return CollectionUtils.isNotEmpty(tCompanyInfos) ? tCompanyInfos.get(0) : new TCompanyInfo();
    }
}
